package cn.bonoon.controllers.fmm;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.bonoon.controllers.announcement.AnnouncementUtils;
import cn.bonoon.core.plugins.AnnouncementService;
import cn.bonoon.core.plugins.FaqCatalogService;
import cn.bonoon.core.plugins.RoleService;
import cn.bonoon.kernel.events.PanelEvent;
import cn.bonoon.kernel.io.FileManager;
import cn.bonoon.kernel.menus.MenuCheckable;
import cn.bonoon.kernel.menus.MenuViewBuilder;
import cn.bonoon.kernel.web.controllers.AbstractLayoutController;
import cn.bonoon.kernel.web.models.PanelModel;

@Controller
@RequestMapping("s/sys/module")
public class ModuleController extends AbstractLayoutController{

	@Autowired
	private RoleService roleService;
	@Autowired
	private AnnouncementService announcementService;
	@Autowired
	private FaqCatalogService faqCatalogService;
	@Autowired
	private FileManager fileManager;
	
	@Override
	protected void onLoad(PanelEvent event) throws Exception {
		PanelModel model = event.getModel();
		model.addObject("modules", moduleManager.modules());
		event.setVmPath("sys/account/module");
	}

	@RequestMapping(value = "!{key}/view.do", method = GET)
	public String module(Model model, @RequestParam("id") String id){
		try{
			MenuViewBuilder builder = moduleManager.menuBuilder(MenuCheckable.READONLY, false, id);
			model.addAttribute("module", builder.parse());
			model.addAttribute("id", id);
		}catch (Exception e) {
			log(e);
		}
		model.addAttribute("layout", "layout-empty.vm");
		return "sys/account/module-view";
	}

	@RequestMapping(value = "!{key}/roles.do", method = GET)
	public String roles(Model model, @RequestParam("id") String id){
		try{
			model.addAttribute("roles", roleService.moduleRoles(getUser(), id));
		}catch (Exception e) {
			log(e);
		}
		model.addAttribute("layout", "layout-empty.vm");
		return "sys/account/module-view-roles";
	}

	@RequestMapping(value = "!{key}/announcements.do", method = GET)
	public String announcements(Model model, @RequestParam("id") String id){
		try{
			model.addAttribute("content", AnnouncementUtils.parseAnnouncement(announcementService.moduleAnnouncements(getUser(), id, 0), fileManager));
		}catch (Exception e) {
			log(e);
		}
		model.addAttribute("layout", "layout-empty.vm");
		return "sys/sys-announcement";
	}

	@RequestMapping(value = "!{key}/faqs.do", method = GET)
	public String faqs(Model model, @RequestParam("id") String id){
		try{
			model.addAttribute("catalogs", faqCatalogService.moduleCatalogs(getUser(), id));
		}catch (Exception e) {
			log(e);
		}
		model.addAttribute("layout", "layout-empty.vm");
		return "sys/sys-faq-catalog";
	}
}
